package com.cybertron.ironhide.manage.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import com.cybertron.ironhide.manage.common.base.Pager;
import com.cybertron.ironhide.manage.domain.SeedUrl;
import com.cybertron.ironhide.manage.persistence.CategoryMapper;
import com.cybertron.ironhide.manage.persistence.SeedUrlMapper;
import com.cybertron.ironhide.manage.service.SeedUrlService;
import com.cybertron.ironhide.manage.util.CommonUtil;
import com.cybertron.ironhide.manage.util.UrlUtil;

@Service
public class SeedUrlServiceImpl implements SeedUrlService {

	@Resource
	private SeedUrlMapper seedUrlMapper;

	@Resource
	private CategoryMapper categoryMapper;

	@Override
	public void insertSelective(SeedUrl record) {
		record.setCreateDate(new Date());
		record.setModifyDate(new Date());
		record.setId(CommonUtil.getUUID());
		seedUrlMapper.insertSelective(record);
	}

	@Override
	public List<SeedUrl> selectByCategoryId(String id, Pager pager) {
		return seedUrlMapper.selectByCategoryId(id, pager);
	}

	@Override
	public void deleteByPrimaryKey(String id) {
		seedUrlMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void insertSelective(File seedFile) {
		Workbook workbook = null;
		try {
			workbook = new XSSFWorkbook(new FileInputStream(seedFile));
		} catch (Exception ex) {
			try {
				workbook = new HSSFWorkbook(new FileInputStream(seedFile));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (workbook == null) {
			return;
		}
		for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
			Sheet sheet = workbook.getSheetAt(numSheet);
			if (sheet == null) {
				continue;
			}
			String firstCateId = null;
			String secondCateId = null;
			for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
				Row row = sheet.getRow(rowNum);
				if (row != null && row.getCell(0) != null) {
					Cell cellTitle = row.getCell(0);
					cellTitle.setCellType(Cell.CELL_TYPE_STRING);
					if (StringUtils.isNotBlank(StringUtils.trim(cellTitle
							.getStringCellValue()))) {
						firstCateId = categoryMapper
								.selectIdByTitle(StringUtils.trim(cellTitle
										.getStringCellValue()));
					}

				}
				if (row != null && row.getCell(1) != null) {
					Cell cellTitle = row.getCell(1);
					cellTitle.setCellType(Cell.CELL_TYPE_STRING);
					if (StringUtils.isNotBlank(StringUtils.trim(cellTitle
							.getStringCellValue()))) {
						secondCateId = categoryMapper.selectIdByParentId(
								firstCateId, StringUtils.trim(cellTitle
										.getStringCellValue()));
					}
				}
				if (row == null || row.getCell(2) == null) {
					continue;
				}
				Cell cellCateTitle = row.getCell(2);
				String cateTitle = null;
				cellCateTitle.setCellType(Cell.CELL_TYPE_STRING);
				cateTitle = StringUtils
						.trim(cellCateTitle.getStringCellValue());
				if (StringUtils.isBlank(cateTitle)) {
					continue;
				}
				String categoryId = categoryMapper.selectIdByParentId(
						secondCateId, cateTitle);
				for (int cellNum = 3; cellNum <= row.getLastCellNum(); cellNum++) {
					Cell cell = row.getCell(cellNum);
					if (cell == null) {
						continue;
					}
					cell.setCellType(Cell.CELL_TYPE_STRING);
					String url = cell.getStringCellValue();
					if (StringUtils.isBlank(url)) {
						continue;
					}
					url = StringUtils.trim(StringUtils.trim(url.replace(
							String.valueOf((char) 160), "")));
					SeedUrl seedUrl = seedUrlMapper.selectByUrlAndCategoryId(
							url, categoryId);
					if (seedUrl == null) {
						seedUrl = new SeedUrl();
						seedUrl.setId(CommonUtil.getUUID());
						seedUrl.setCategoryId(categoryId);
						seedUrl.setCreateDate(new Date());
						seedUrl.setModifyDate(new Date());
						seedUrl.setUrl(url);
						seedUrl.setHost(UrlUtil.getHost(url));
						seedUrlMapper.insertSelective(seedUrl);
					} else {
						seedUrl.setCategoryId(categoryId);
						seedUrl.setHost(UrlUtil.getHost(url));
						seedUrl.setModifyDate(new Date());
						seedUrlMapper.updateByPrimaryKeySelective(seedUrl);
					}
				}
			}
		}
	}
}
